package com.swufestu.second;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class RateList2Activity extends AppCompatActivity implements AdapterView.OnItemClickListener {
//public class RateList2Activity extends AppCompatActivity{
    Handler handler;
    private static final String TAG = "onItemClick";
    ListView list2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);
        list2 = findViewById(R.id.mylist2);
        //ProgressBar bar = findViewById(R.id.progressBar4);

        handler = new Handler(Looper.myLooper()){

            @Override
            public void handleMessage(Message msg){
                if(msg.what==9){
                    //List<String> listre = (List<String>)msg.obj;
                    //HashMap<String,String> map = (HashMap<String,String>)msg.obj;
                    ArrayList<HashMap<String,String>> listItems = (ArrayList<HashMap<String,String>>)msg.obj;
                    Log.i(TAG, "handleMessage: listItems"+listItems);

                    //ListAdapter adapter = new ArrayAdapter<String>(RateList2Activity.this, android.R.layout.simple_list_item_1,listre);
                    SimpleAdapter listAdapter = new SimpleAdapter(
                            RateList2Activity.this,
                            listItems,
                            R.layout.list_item,
                            new String[]{"country","rate"},
                            new int[]{R.id.country,R.id.rate});
                    /*MyAdapter listAdapter = new MyAdapter(
                            RateList2Activity.this,
                            R.layout.list_item,
                            listItems);*/
                    list2.setAdapter(listAdapter);
                    //切换显示
                    //bar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };

//        MyTast dt=new MyTast(handler);
//        Thread t=new Thread(dt);
//        t.start();

        RateTask1 dt=new RateTask1(handler);
        Thread t=new Thread(dt);
        t.start();

        list2.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Object itemAtPosition = list2.getItemAtPosition(position);
        HashMap<String,String> map = (HashMap<String,String>)itemAtPosition;
        String name = map.get("name");
        String rate = map.get("rate");
        Log.i(TAG, "onItemClick: name:"+name);
        Log.i(TAG, "onItemClick: rate"+rate);

        TextView title = (TextView) view.findViewById(R.id.country);
        TextView detail = (TextView) view.findViewById(R.id.rate);
        String title1 = String.valueOf(title.getText());
        String detail1 = String.valueOf(detail.getText());
        Log.i(TAG, "onItemClick: title:"+title1);
        Log.i(TAG, "onItemClick: detail"+detail1);

        Intent config = new Intent(this,listChangeRate.class);
        Bundle bdl = new Bundle();
        bdl.putString("country",title1);
        bdl.putString("rate",detail1);
        config.putExtras(bdl);
        //startActivity(config);
        startActivityForResult(config,1);
    }

}